﻿using System;
using System.Transactions;
using System.Web.UI.WebControls;
using Ones.ServiceCategoryManagement;
using Ones.ServiceNewsManagement;
//using XpressNews_BackEnd.ServiceHumanResourceManagement;
//using CSASPNETMessageBox;

namespace Ones.Editorial.Writer
{
    public partial class NewsCompose : System.Web.UI.Page
    {
        private ServiceNewsManagement.ServiceNewsManagementClient srvNewsMngmt = null;
        private ServiceCategoryManagement.ServiceCategoryManagementClient srvCatMngmt = null;
        private ServiceHumanResourceManagement.ServiceHumanResourceManagementClient srvHRMngmt = null;
        string userAccount = string.Empty;

        enum processStatus
        {
            Approved,
            Edited,
            Posted,
            RejectedA,
            RejectedE
        }

        enum processDirection
        {
            Post,
            Reject
        }

        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
            successfulMsg.Visible = false;
            failMsg.Visible = false;
            if (!Page.IsPostBack)
            {
                this.BindNews();
                //RemoveButton("FlashManager");
            }

            if (Session["user"] != null)
            {
                userAccount = Session["user"].ToString();
            }
            else { userAccount = "emp000013"; }

            //var empDetail=srvHRMngmt.searchEmployeeById(userAccount);
            //if (empDetail != null)
            //{
            //    lblAuthor.InnerText = empDetail.empFirstName + empDetail.empLastName;
            //}
            //else { lblAuthor.InnerText = userAccount; }

            lblAuthor.InnerText = userAccount;
        }

        protected void NewsCompose_Confirm(object sender, CommandEventArgs e)
        {
            //string alertNewsAdded = "alert('News has been added successfully!');";
            string confirmNewsComposeCancel = "if(confirm('Are you sure you want to cancel composing news?')==true){window.location='NewsDisplay.aspx';}";

            switch (e.CommandName.ToString())
            {
                case "cmdPost":
                    this.ComposeNews();
                    //ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", alertNewsAdded, true);
                    break;
                case "cmdCancel":
                    bool result = this.CancelNewsCompose();
                    if (result)
                    {
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "Confirm", confirmNewsComposeCancel, true);
                    }
                    else { Response.Redirect("~/Editorial/Writer/NewsDisplay.aspx"); }
                    break;
                default:
                    break;
            }
        } 

        #endregion

        #region Methods
        private void BindNews()
        {
            srvCatMngmt = new ServiceCategoryManagementClient("catWsHttpBinding");
            ddlCategory.DataSource = srvCatMngmt.searchAllCategories();
            ddlCategory.DataBind();
        }

        private bool CancelNewsCompose()
        {
            if (txtTitle.Value.Equals("") || txtNewsIntro.Value.Equals("") || NewsEditor.Text.Equals(""))
                return true;
            return false;
        }

        private void ComposeNews()
        {
            DateTime currentDate = System.DateTime.Now;

            using (TransactionScope trans = new TransactionScope(TransactionScopeOption.RequiresNew))
            {
                try
                {
                    srvNewsMngmt = new ServiceNewsManagementClient("newsWsHttpBinding");

                    Ones.ServiceNewsManagement.News n = new Ones.ServiceNewsManagement.News();
                    n.newsTitle = txtTitle.Value;;
                    n.catId = ddlCategory.SelectedValue;
                    n.newsIntroduceText = txtNewsIntro.Value;
                    n.newsContent = NewsEditor.Text;
                    n.postingDate = currentDate;
                    n.author = userAccount;
                    n.processStatus = processStatus.Posted.ToString();
                    n.newsExclusion = false;
                    n.verified = false;
                    //n.numberOfView = 1;

                    Ones.ServiceNewsManagement.NewsVersion nv = new Ones.ServiceNewsManagement.NewsVersion();
                    nv.verSender = userAccount;
                    nv.verProcessDirection = processDirection.Post.ToString();
                    nv.verDate = currentDate;

                    bool result = srvNewsMngmt.ComposeNews(n, nv);
                    if (result)
                    {
                        trans.Complete();
                        successfulMsg.Visible = true;
                        failMsg.Visible = false;
                    }
                }
                catch (TransactionException ex)
                {
                    trans.Dispose();
                    successfulMsg.Visible = false;
                    failMsg.Visible = true;
                }
            }
        }

        private void BoundData()
        {
            lblTitle.InnerText = txtTitle.Value;
        }

        public void RemoveButton(string name)
        {
            //foreach (Telerik.Web.UI.EditorToolGroup group in NewsEditor.Tools)
            //{
            //    EditorTool tool = group.FindTool(name);
            //    if (tool != null)
            //    {
            //        group.Tools.Remove(tool);
            //    }
            //}
        }

        #endregion

    }
}